package com.suxin.websocket.session;

import javax.websocket.Session;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * @author Tang
 * @classname SimpleSessionPath
 * @description [  ]
 * @date 2022/3/24 11:55
 */
public class SimpleSessionPath implements SessionPath {

    private final List<String> paramsPath;

    private String userKey;

    public SimpleSessionPath(String... params) {
        paramsPath = Arrays.stream(params).collect(Collectors.toList());
    }

    @Override
    public String buildKey(Session session) {
        Map<String, String> pathParameters = session.getPathParameters();
        StringJoiner stringJoiner = new StringJoiner("-");
        pathParameters.forEach((key,value) -> {
            stringJoiner.add(pathParameters.get(key));
        });
        this.userKey = stringJoiner.toString();
        return getKey();
    }

    @Override
    public String getKey() {
        return this.userKey;
    }

    public List<String> getParamsPath() {
        return paramsPath;
    }
}